package JackAnalyzer;
import java.io.File;

public class JackAnalyzer {
	
	private static final String USAGE_MASSAGE = "Usage: JackAnalyzer <%FILE PATH%.jack | %PATH% of folder>";
	
	public static boolean DEBUG = false;
	public static boolean DEBUG_T = false;
	public static boolean UNIT_TEST = true;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if(UNIT_TEST){
			System.out.println("Start unit testing.. (if you see that i forgot to turn off the UNIT_TEST flag)");
//			generateXML(new File("tests/GameBoardPush.jack"));
			generateFromFolder(new File("tests"));
			return;
		}
		if(args.length != 1){
			System.err.println(USAGE_MASSAGE);
			System.exit(1);
		}
		File file = new File(args[0]);
		if(file.isFile()){
			generateXML(file);
		}else if(file.isDirectory()){
			generateFromFolder(file);
		}else
		  throw new RuntimeException(USAGE_MASSAGE);
	}
	
	private static void generateFromFolder(File dir){
		for (File currentFile : dir.listFiles()) {
			if (currentFile.isFile() && currentFile.getName().endsWith(".jack")) {
				if(UNIT_TEST){
					System.out.println("Starting: " + currentFile.getName());
					if(currentFile.getName().contains("GameBoardPush"))
						continue;
				}
				generateXML(currentFile);

			}
		}
	}
	
	private static void generateXML(File file){
		CompilationEngine ce = new CompilationEngine(file , file.getName().replace("jack", "xml"));
		ce.compileClass();
		ce.close();
	}
	
	
}
